﻿using Jupiter.Infrastructure.Attributes;
using Jupiter.Infrastructure.Cache.Interfaces;
using Jupiter.Infrastructure.Domain.SqlSugar.Basic;
using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.DataPermissions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Localization;
using Wips.AppService.CommonViewModels;
using Wips.AppService.Publisher.AgentServerPermissions;
using Wips.AppService.Publisher.AgentServerPermissions.Responses;
using Wips.Domain.Models;

namespace Wips.AppService.SqlSugar.Publisher
{

    /// <summary>
    /// 服务器授权服务
    /// </summary>
    [ServiceComponent]
    public class AgentServerPermissionAppService : AppServiceQueryBase<AgentServer>, IAgentServerPermissionAppService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="securityContextAccessor"></param>
        /// <param name="configuration"></param>
        /// <param name="cacheManager"></param>
        /// <param name="permission"></param>
        /// <param name="stringLocalizer"></param>
        public AgentServerPermissionAppService(ISqlSugarClient dbContext, ISecurityContextAccessor securityContextAccessor, IConfiguration? configuration, ICacheManager? cacheManager, IDataPermission? permission, IStringLocalizer? stringLocalizer) : base(dbContext, securityContextAccessor, configuration, cacheManager, permission, stringLocalizer)
        {
        }


        /// <summary>
        /// 获取服务器授权列表 by userId
        /// </summary>
        /// <returns></returns>
        public async Task<GetPermissionResponse> GetPermissionListAsync(string? userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                userId = UserId!;
            }
            var result = new GetPermissionResponse();
            var permissionList = await Query<AgentServerPermission>()
                .Includes(c => c.AgentServer)
                .Includes(c => c.AgentServerConfig)
                .Where(c => c.UserId == userId)
                    .ToListAsync();


            var list = await Query()
              .Includes(c => c.AgentServerConfigs)
               .ToListAsync();

            result.AgentServers = list.Select(c => new CheckboxViewModel
            {
                Label = c.ServerName + "[" + c.ServerKey + "]",
                Value = c.Id!,

            }).ToList();

            result.AgentServerConfigs = list.SelectMany(c => c.AgentServerConfigs!).Select(c => new CheckboxViewModel
            {
                Label = c.ServerName,
                Value = c.Id!,
                Extend = c.AgentServerId
            }).ToList();

            foreach (var item in result.AgentServers)
            {
                var permission = permissionList.Where(c => c.AgentServerId == item.Value).FirstOrDefault();
                item.Checked = permission != null;
                foreach (var c in result.AgentServerConfigs)
                {
                    if (c.Extend == item.Value)
                    {
                        if (item.Checked)
                        {
                            c.Checked = true;
                            c.Disabled = true;
                        }
                        else
                        {
                            var configPermission = permissionList.Where(pc => pc.AgentServerConfigId == c.Value).FirstOrDefault();
                            c.Checked = configPermission != null;
                        }
                    }
                }
            }

            return result;
        }

    }
}
